/**
 * 请求接口
 */
import http from './ajax';
import NavigationService from '../components/NavigationService';
import { LoginToken } from './AdapterUtil';

const commonPost = (url, params = {}) => new Promise((resolve, reject) => {
  http
    .post({
      url,
      params,
    })
    .then((res) => {
      if (
        res
          && res.httpCode == 200
          && res.code
          && (res.code === 'authorizationExpired' || res.code === 'unAuthorized')
      ) {
        // 清空token
        global.storage
          .save({
            key: LoginToken,
            data: '',
            expires: null,
          })
          .then((ret) => {
            NavigationService.reset('login');
            global.token = null;
          })
          .catch((err) => {});
        return reject({
          code: res.code,
          msg: '登录超时，请重新登录',
        });
      } if (res && res.httpCode == 200 && res.code === 'success') {
        return resolve(res.data);
      }
      return reject({
        code: res.httpCode,
        msg: res.message || '请求失败，请稍后重试',
      });
    })
    .catch(res => reject({
      code: res.message,
      msg: '请求失败，请稍后重试',
    }));
});

const commonGet = (url, params = {}) => new Promise((resolve, reject) => {
  http
    .get({
      url,
      params,
    })
    .then((res) => {
      if (
        res
          && res.httpCode == 200
          && res.code
          && (res.code === 'authorizationExpired' || res.code === 'unAuthorized')
      ) {
        // 清空token
        global.storage
          .save({
            key: LoginToken,
            data: '',
            expires: null,
          })
          .then((ret) => {
            NavigationService.reset('login');
            global.token = null;
          })
          .catch((err) => {});
        return reject({
          code: res.code,
          msg: '登录超时，请重新登录',
        });
      } if (res && res.httpCode == 200 && res.code === 'success') {
        return resolve(res.data);
      }
      return reject({
        code: res.httpCode,
        msg: res.message || '请求失败，请稍后重试',
      });
    })
    .catch(res => reject({
      code: res.message,
      msg: '请求失败，请稍后重试',
    }));
});

/**
 * 重置登录密码的 发送验证码
 */
export const getMessageCode = (params = {}) => commonGet('/receiptapp/userInfo/obtainVerificationCore', params);
/**
 * 重置支付密码的 发送验证码
 */
export const getPayPasswordVCode = (params = {}) => commonGet('/receiptapp/companyManage/obtainVerificationCore', params);

/**
 * 重置登录密码的 校验验证码
 */
export const verificationCode = (params = {}) => commonGet('/receiptapp/userInfo/verificationCoreConfirm', params);

/**
 * 重置支付密码的 校验验证码
 */
export const verificationPayVCode = (params = {}) => commonGet('/receiptapp/companyManage/verificationCoreConfirm', params);
/**
 * 验证码重置密码
 */
export const vCodeResetPassword = (params = {}) => commonPost('/receiptapp/userInfo/resetPassword', params);
/**
 * 旧密码重置密码
 */
export const updateLoginPassword = (params = {}) => commonPost('/receiptapp/userInfo/updateLoginPassword', params);

/**
 * 验证码重置支付密码
 */
export const vCodeResetPayPassword = (params = {}) => commonPost('/receiptapp/companyManage/resetPassword', params);
/**
 * 旧密码重置支付密码 验证旧密码
 */
export const verificationPayPassword = (params = {}) => commonPost(
  '/receiptapp/companyManage/verificationTradePassword',
  params,
);
/**
 * 无密码时设置支付密码
 */
export const setPayPassword = (params = {}) => commonPost('/receiptapp/companyManage/setTradePassword', params);

/**
 * 检查用户是否有支付密码
 */
export const havePayPassword = (params = {}) => commonGet('/receiptapp/companyManage/payPassword', params);

/**
 * 查询出账账单
 */
export const queryBill = (params = {}) => commonPost('/receiptapp/billRecord/queryGeneratedBillRecord', params);
/**
 * 查询未出账账单
 */
export const queryYetGeneratedBill = (params = {}) => commonPost(
  '/receiptapp/billRecord/queryYetGeneratedBillRecord',
  params,
);

// 获取搜索汽配商列表
export const getSearchListData = (params = {}) => commonGet('/receiptapp/companyManage/queryReceiptapps', params);

// 辛巴支付
export const xinbaPay = (params = {}) => commonPost('/receiptapp/orderManage/pay', params);

// 获取公司查询列表
export const queryCompany = (params = {}) => commonGet('/receiptapp/companyManage/queryCompany', params);

/* 获取七牛token */
export const getQiniuToken = (params = {}) => commonGet('/receiptapp/qiniuyun', params);

/* 获取服务协议 */
export const queryUserProtocal = (params = {}) => commonGet('/receiptapp/userInfo/queryUserProtocal', params);

/* 获取企业信息 */
export const getEnterpriseInformation = (params = {}) => commonGet(
  '/receiptapp/companyManage/queryEnterpriseInformation',
  params,
);

/* 更新企业信息 */
export const upEnterpriseInformation = (params = {}) => commonPost(
  '/receiptapp/companyManage/upEnterpriseInformation',
  params,
);

/* 获取省 */
export const getProvince = (params = {}) => commonGet('/receiptapp/administrativeArea/getProvince', params);

/* 获取市 */
export const getCity = (params = {}) => commonGet('/receiptapp/administrativeArea/getCity', params);

/* 获取县 */
export const getCountry = (params = {}) => commonGet('/receiptapp/administrativeArea/getCountry', params);

/* 订单详情 */
export const orderDetails = (params = {}) => commonGet('/receiptapp/orderManage/orderDetails', params);

/* 获取开票信息 */
export const getInvoiceInfo = (params = {}) => commonGet('/receiptapp/companyManage/queryInvoiceInformation', params);

/* 保存开票信息 */
export const saveInvoiceInfo = (params = {}) => commonPost('/receiptapp/companyManage/upInvoiceInformation', params);

/* 查询子账号信息 */
export const getSubUserInfo = (params = {}) => commonGet('/receiptapp/userInfo/querySubUserInfos', params);

/* 更新子账号信息 */
export const saveSubUserInfo = (params = {}) => commonPost('/receiptapp/userInfo/insertSubUserInfo', params);

/* 我的额度 */
export const getMylimit = (params = {}) => commonGet('/receiptapp/companyManage/myQuota', params);

/* 查询配送人 */
export const getDeliveryList = (params = {}) => commonGet('/receiptapp/delivery/queryDelivery', params);

/* 立即还款 */
export const immediateRepayment = (params = {}) => commonPost('/receiptapp/companyManage/immediateRepayment', params);

/* 提前还款 */
export const prepayment = (params = {}) => commonPost('/receiptapp/companyManage/prepayment', params);

/* 确定收货 */
export const confirmReceipt = (params = {}) => commonPost('/receiptapp/orderManage/confirmReceipt', params);

/* 拒收 */
export const refuseOrder = (params = {}) => commonPost('/receiptapp/orderManage/rejectOrder', params);

/* 添加备注图片和备注信息 */
export const addPayPicAndRemarks = (params = {}) => commonPost('/receiptapp/orderManage/AddPayPicAndRemarks', params);

/* 更新用户信息 */
export const updateUserInfo = (params = {}) => commonPost('/receiptapp/userInfo/updateUserInfo', params);

/* 更新用户信息 */
export const queryUsedMoney = (params = {}) => commonPost('/receiptapp/billRecord/queryUsedMoney', params);
